﻿using LiWi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace LiWi
{
    public partial class FormMain : Form
    {

        ~FormMain() {

        }

        int[] axisList = { 0, 1, 2, 3, 4 };

        public FormMain()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            
            //展示版本
            Version version=Assembly.GetExecutingAssembly().GetName().Version;
            
            this.Text = $"LiWiDemo V{version}";

           

            connectForm.TopLevel = false;
            connectForm.FormBorderStyle = FormBorderStyle.None;
            connectForm.Dock = DockStyle.Fill;

            demo1Form.TopLevel = false;
            demo1Form.FormBorderStyle = FormBorderStyle.None;
            demo1Form.Dock = DockStyle.Fill;

            demo2Form.TopLevel = false;
            demo2Form.FormBorderStyle = FormBorderStyle.None;
            demo2Form.Dock = DockStyle.Fill;

            demo3Form.TopLevel = false;
            demo3Form.FormBorderStyle = FormBorderStyle.None;
            demo3Form.Dock = DockStyle.Fill;

            demo4Form.TopLevel = false;
            demo4Form.FormBorderStyle = FormBorderStyle.None;
            demo4Form.Dock = DockStyle.Fill;

            demo5Form.TopLevel = false;
            demo5Form.FormBorderStyle = FormBorderStyle.None;
            demo5Form.Dock = DockStyle.Fill;

            demo6Form.TopLevel = false;
            demo6Form.FormBorderStyle = FormBorderStyle.None;
            demo6Form.Dock = DockStyle.Fill;

            demo7Form.TopLevel = false;
            demo7Form.FormBorderStyle = FormBorderStyle.None;
            demo7Form.Dock = DockStyle.Fill;

            demo8Form.TopLevel = false;
            demo8Form.FormBorderStyle = FormBorderStyle.None;
            demo8Form.Dock = DockStyle.Fill;

            demo9Form.TopLevel = false;
            demo9Form.FormBorderStyle = FormBorderStyle.None;
            demo9Form.Dock = DockStyle.Fill;

            demo10Form.TopLevel = false;
            demo10Form.FormBorderStyle = FormBorderStyle.None;
            demo10Form.Dock = DockStyle.Fill;

            demo11Form.TopLevel = false;
            demo11Form.FormBorderStyle = FormBorderStyle.None;
            demo11Form.Dock = DockStyle.Fill;

            demo12Form.TopLevel = false;
            demo12Form.FormBorderStyle = FormBorderStyle.None;
            demo12Form.Dock = DockStyle.Fill;

            demo13Form.TopLevel = false;
            demo13Form.FormBorderStyle = FormBorderStyle.None;
            demo13Form.Dock = DockStyle.Fill;

            demo14Form.TopLevel = false;
            demo14Form.FormBorderStyle = FormBorderStyle.None;
            demo14Form.Dock = DockStyle.Fill;


            tabPageConnect.Controls.Add(connectForm);
            tabPage1.Controls.Add(demo1Form);
            tabPage2.Controls.Add(demo2Form);
            tabPage3.Controls.Add(demo3Form);
            tabPage5.Controls.Add(demo4Form);
            tabPage4.Controls.Add(demo5Form);
            tabPage6.Controls.Add(demo6Form);
            tabPage7.Controls.Add(demo7Form);
            tabPage8.Controls.Add(demo8Form);
            tabPage9.Controls.Add(demo9Form);
            tabPage10.Controls.Add(demo10Form);
            tabPage11.Controls.Add(demo11Form);
            tabPage12.Controls.Add(demo12Form);
            tabPage13.Controls.Add(demo13Form);
            tabPage14.Controls.Add(demo14Form);

            
            connectForm.Show();
            demo1Form.Show();
            demo2Form.Show();
            demo3Form.Show();
            demo4Form.Show();
            demo5Form.Show();
            demo6Form.Show();
            demo7Form.Show();
            demo8Form.Show();
            demo9Form.Show();
            
            demo10Form.Show();
            demo11Form.Show();
            demo12Form.Show();
            demo13Form.Show();
            demo14Form.Show();
            // windowsAdapt.SetForm(this);

            WindowsAdapt.SetResizeEnable(true);
            //this.WindowState = FormWindowState.Maximized;


        }

        ConnectForm connectForm = new ConnectForm();
        直线插补 demo1Form=new 直线插补();
        圆弧插补 demo2Form = new 圆弧插补();
        螺旋插补 demo3Form = new 螺旋插补();
        回原点   demo4Form = new 回原点();
        Io控制 demo5Form = new Io控制();
        位置比较输出与锁存 demo6Form = new 位置比较输出与锁存();
        报警处理 demo7Form = new 报警处理();
        连续插补 demo8Form = new 连续插补();
        手轮测试 demo9Form = new 手轮测试();
        凸轮跟随 demo10Form = new 凸轮跟随();
        自动电子凸轮运动 demo11Form = new 自动电子凸轮运动();
        上位机和Basic交互 demo12Form = new 上位机和Basic交互();
        自定义指令封装 demo13Form = new 自定义指令封装();
        点动模式 demo14Form = new 点动模式();

        


        private void tabParamSet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Data.handle == (IntPtr)0&& tabParamSet.SelectedIndex != 0)
            {
                tabParamSet.SelectedIndex = 0;
                MessageBox.Show("请先连接控制卡!!!");
            }
        }



        /// <summary>
        /// 点位更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(Data.handle != (IntPtr)0)
            {
                foreach (var item in axisList)
                {
                    float value = 0;
                    int ret = Shell.PMC_GetDpos(Data.handle, item, ref value);
                    if (ret == 0)
                    {
                        try
                        {
                            if (item == 0) labPos0.Text = value.ToString();
                            else if (item == 1) labPos1.Text = value.ToString();
                            else if (item == 2) labPos2.Text = value.ToString();
                            else if (item == 3) labPos3.Text = value.ToString();
                            else if (item == 4) labPos4.Text = value.ToString();
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show(ex.ToString());
                        }
                    }
                }
            }
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Shell.PMC_Close(Data.handle);
            //connectForm.Close();
            //demo1Form.Close();
            //demo2Form.Close();
            //demo3Form.Close();
            //demo4Form.Close();
            //demo5Form.Close();
            //demo6Form.Close();
            //demo7Form.Close();
            //demo8Form.Close();
            //demo9Form.Close();
            //demo10Form.Close();
            //demo11Form.Close();
            //demo12Form.Close();
            //demo13Form.Close();
            //demo14Form.Close();
            Application.Exit();
        }
    }
}
